﻿
//-----------------------------------------------------------------------
// <copyright file="Address.cs" company="FastNET Services">
//     Copyright (c) 2007-2011. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using FastNet.BusinessSystem.Objects;
using FastNet.BusinessSystem.Rules;
using FastNet.RepositorySystem;
using FastNet.RepositorySystem.Interfaces;
using SampleRepository.Entities;
using SampleRepository.Repositories;

#endregion

namespace SampleBusiness.Objects
{
    /// <summary>
    /// Business Object for Address
    /// </summary>
    public class Address : IdentityBusinessObject<IAddressData, IAddressRepository>
    {

        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of Address
        /// </summary>
        public Address()
            : base(new RepositoryFactory())
        {
            AddBusinessRules();
        }

        /// <summary>
        /// Initializes a new instance of Address
        /// </summary>
        internal Address(IRepositoryFactory factory, IAddressData data)
            : base(factory, data)
        {
            AddBusinessRules();
        }

        #endregion

        #region Business Rules

        /// <summary>
        /// Handles adding of the business rules for validation to the instance.
        /// </summary>
        private void AddBusinessRules()
        {
            BusinessRules.Add(new BusinessLambdaRule("AddressLine1_IsNotNullOrEmpty", "AddressLine1", "Address line 1 cannot be null or empty", () => !string.IsNullOrEmpty(Data.AddressLine1)));

            BusinessRules.Add(new BusinessLambdaRule("City_IsNotNullOrEmpty", "City", "City cannot be null or empty", () => !string.IsNullOrEmpty(Data.City)));
            BusinessRules.Add(new BusinessLambdaRule("State_IsNotNullOrEmpty", "State", "State cannot be null or empty", () => !string.IsNullOrEmpty(Data.State)));
            BusinessRules.Add(new BusinessLambdaRule("ZipCode_IsNotNullOrEmpty", "ZipCode", "Zip code cannot be null or empty", () => !string.IsNullOrEmpty(Data.ZipCode)));

            BusinessRules.Add(new BusinessLambdaRule("AddressLine1_LengthMax", "AddressLine1", "Address line 1 cannot be more than 30 characters", () => Data.AddressLine1 != null && Data.AddressLine1.Length <= 30));
            BusinessRules.Add(new BusinessLambdaRule("AddressLine2_LengthMax", "AddressLine2", "Address line 2 cannot be more than 30 characters", () => Data.AddressLine2 != null && Data.AddressLine2.Length <= 30));
            BusinessRules.Add(new BusinessLambdaRule("AddressLine3_LengthMax", "AddressLine3", "Address line 3 cannot be more than 30 characters", () => Data.AddressLine3 != null && Data.AddressLine3.Length <= 30));

            BusinessRules.Add(new BusinessLambdaRule("City_LengthMax", "City", "City cannot be more than 20 characters", () => Data.City != null && Data.City.Length <= 20));
            BusinessRules.Add(new BusinessLambdaRule("State_LengthMax", "State", "State cannot be more than 2 characters", () => Data.State != null && Data.State.Length <= 2));
            BusinessRules.Add(new BusinessLambdaRule("ZipCode_LengthMax", "ZipCode", "Zip code cannot be more than 10 characters", () => Data.ZipCode != null && Data.ZipCode.Length <= 10));
        }

        #endregion

        #region State Control

        /// <summary>
        /// Gets a value indicating whether or not the current object can be modified
        /// </summary>
        public override bool CanModify { get { return true; } }

        /// <summary>
        /// Gets a value indicating whether or not the current object can be saved
        /// </summary>
        public override bool CanSave { get { return true; } }

        /// <summary>
        /// Gets a value indicating whether or not the current object can be deleted
        /// </summary>
        public override bool CanDelete { get { return true; } }

        #endregion

        public string AddressLine1
        {
            get { return Data.AddressLine1; }
            set
            {
                Data.AddressLine1 = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("AddressLine1"));
            }
        }

        public string AddressLine2
        {
            get { return Data.AddressLine2; }
            set
            {
                Data.AddressLine2 = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("AddressLine2"));
            }
        }

        public string AddressLine3
        {
            get { return Data.AddressLine3; }
            set
            {
                Data.AddressLine3 = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("AddressLine3"));
            }
        }

        public string City
        {
            get { return Data.City; }
            set
            {
                Data.City = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("City"));
            }
        }

        public string State
        {
            get { return Data.State; }
            set
            {
                Data.State = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("State"));
            }
        }

        public string ZipCode
        {
            get { return Data.ZipCode; }
            set
            {
                Data.ZipCode = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("ZipCode"));
            }
        }

    }
}
